// 这个文件专门用来存放关于首页的所有接口
const Router = require('koa-router');
const order = new Router();
const db = require('../utils/db');
const bodyparser = require('koa-bodyparser')
//调用下面这个中间件，就可以拿到前端post过来的数据
order.use(bodyparser());
// 写对应的接口
order.post('/', async (ctx, next) => {
    var i = 0;
    console.log("接收到post请求");
    await next();
    const data = ctx.request.body;
    // console.log(data.orderList);
    // console.log(JSON.stringify(data.orderList));
    var orderList = JSON.stringify(data.orderList);
    let searchSql = `insert into orderNew (openid,orderList, buyname,address,phone,createtime,totalMoney,note,status) values ` +
        `('${data.openid}','${orderList}','${data.buyname}','${data.address}','${data.phone}',` +
        `'${data.time}','${data.totalMoney}','${data.note}','${data.status}')`;
        let mydata = await new Promise((resolve, reject) => {
            // 访问数据库，拿到banner这个数据给前端
            return db.query(searchSql, (err, data) => {
                if (err) throw err;
                console.log(data);
                resolve(data);
            });
        })
        ctx.body = mydata;
        console.log('成功插入数据！')
})

order.post('/changeStatus', async (ctx, next) => {
    var i = 0;
    console.log("接收到post请求");
    await next();
    const data = ctx.request.body;
    console.log(data.orderNewid);
    let searchSql = `update orderNew set status="${data.status}" where orderNewid="${data.orderNewid}"`;
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(searchSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
    console.log('成功更新数据！')

})
// 待支付
// order.get('/noPaid', async (ctx) => {
//     let mydata=await new Promise((resolve, reject) => {
//         // 访问数据库，拿到banner这个数据给前端
//         return db.query(`select * from orderNew where status=-1`, (err, data) => {
//             if (err) throw err;
//             console.log(data);
//             resolve(data);
//         });
//     })
//     ctx.body = mydata;
// })
order.post('/incomplete', async (ctx,next) => {
    const data=ctx.request.body;
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from orderNew where status='${data.status}'and openid='${data.openid}'`, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
})
// 待发货
// order.get('/noDelivery', async (ctx) => {
//     let mydata=await new Promise((resolve, reject) => {
//         // 访问数据库，拿到banner这个数据给前端
//         return db.query(`select * from orderNew where status=0`, (err, data) => {
//             if (err) throw err;
//             console.log(data);
//             resolve(data);
//         });
//     })
//     ctx.body = mydata;
// })

// 待收货
// order.get('/noReceived', async (ctx) => {
//     let mydata=await new Promise((resolve, reject) => {
//         // 访问数据库，拿到banner这个数据给前端
//         return db.query(`select * from orderNew where status=1`, (err, data) => {
//             if (err) throw err;
//             console.log(data);
//             resolve(data);
//         });
//     })
//     ctx.body = mydata;
// })
// 待评价
// order.get('/noEvaluate', async (ctx) => {
//     let mydata=await new Promise((resolve, reject) => {
//         // 访问数据库，拿到banner这个数据给前端
//         return db.query(`select * from orderNew where status=2`, (err, data) => {
//             if (err) throw err;
//             console.log(data);
//             resolve(data);
//         });
//     })
//     ctx.body = mydata;
// })
// 已完成
// order.get('/accomplish', async (ctx) => {
//     let mydata=await new Promise((resolve, reject) => {
//         // 访问数据库，拿到banner这个数据给前端
//         return db.query(`select * from orderNew where status=3`, (err, data) => {
//             if (err) throw err;
//             console.log(data);
//             resolve(data);
//         });
//     })
//     ctx.body = mydata;
// })
// 添加评价
order.post('/addComment',async (ctx, next) => {
    console.log("接收到post请求");
    // await next();
    const data = ctx.request.body;
    console.log(data);
    let searchSql = `insert into commentMenu (openid,goodsid, comment,avatarUrl,nickName,goodsName,time,goodsimg) `
        +`values('${data.openid}','${data.goodsId}','${data.comment}','${data.avatarUrl}','${data.nickName}','${data.goodsName}','${data.time}','${data.goodsimg}')`;
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(searchSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
    console.log('成功评论！')
})
// deleteOrder
order.post('/deleteOrder', async (ctx, next) => {
    console.log("接收到post的删除订单请求");
    const data = ctx.request.body;
    console.log(data.orderNewid);
    let searchSql = `delete from orderNew where orderNewid="${data.orderNewid}"`;
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(searchSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
    console.log('成功删除订单数据！')

})
// reduceGoodsNumber减少已经发货的商品的数量
order.post('/reduceGoodsNumber', async (ctx, next) => {
    console.log("接收到post的减少商品数量的请求");
    const data = ctx.request.body;
    console.log(data.goodsid);
    console.log(data.number);
    let searchSql = `update goods set goodsnum=goodsnum-"${data.number}" where goodsid="${data.goodsid}" and goodsnum>="${data.number}"`;
    let mydata = await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(searchSql, (err, data) => {
            if (err) throw err;
            console.log(data);
            resolve(data);
        });
    })
    ctx.body = mydata;
    console.log('成功更新商品数量！')

})


module.exports = order;